//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Interop;
using OpenLS.UI.Interop;

namespace OpenLS.UI
{
    public class SaveFileDialog : FileDialog
    {
        public override bool? ShowDialog(Window window)
        {
            if (!NativeMethods.IsVista)
                return showDialogXp(window);
            return ShowDialogVista(window);
        }

        private bool? ShowDialogVista(Window window)
        {
            IFileSaveDialog d = new IFileSaveDialog();
            if (Filter != null)
            {
                string[] filterArray = Filter.Split('|');
              //  using (FilterCollection f = FilterCollection.Parse(Filter))
                {
                    d.SetFileTypes(filterArray.Length/ 2, filterArray);
                }
            }
            if (DefaultExt != null)
                d.SetDefaultExtension(DefaultExt);
            if (FileName != null)
            {
                d.SetFileName(Path.GetFileName(FileName));
                IShellItem item = GetShellItem(Path.GetDirectoryName(FileName));
                d.SetFolder(item);
            }
            IntPtr handle;
            handle = (window != null)?new WindowInteropHelper(window).Handle:IntPtr.Zero;
            int res = d.Show(handle);
            if (res < 0)
                return false;
            GetFileNames(d);
            return true;
        }
        private static IShellItem GetShellItem(string path)
        {
            Guid riid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE");
            IntPtr result;
            NativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref riid, out result);
            return (IShellItem)Marshal.GetObjectForIUnknown(result);
        }

 

 

        bool? showDialogXp(Window window)
        {
            var dialog = new Microsoft.Win32.SaveFileDialog
                             {
                                 AddExtension = AddExtension,
                                 DefaultExt = DefaultExt,
                                 FileName = FileName,
                                 Filter = Filter,
                                                        };
            bool? result = dialog.ShowDialog(window);
            FileName = dialog.FileName;
            FileNames = dialog.FileNames;
            return result;

        }

        void GetFileNames(IFileSaveDialog d)
        {
            IShellItem item;
            d.GetResult(out item);

            FileName = (GetFileNameFromShellItem(item));
        }


        [SecurityCritical]
        public Stream OpenFile()
        {
            new UIPermission(UIPermissionWindow.AllWindows).Demand();
            string text = (FileNames.Length > 0) ? FileNames[0] : null;
            if (string.IsNullOrEmpty(text))
            {
                throw new InvalidOperationException("no file");
            }
            new FileIOPermission(FileIOPermissionAccess.Append | FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, text).Assert();
            return new FileStream(text, FileMode.Create, FileAccess.ReadWrite);
        }


    }
}
